﻿using MediatR;
using T.Materials.Domain.Dto;
using T.Materials.Domain.Model;
using T.Materials.Read.API.ApplicationRead.Command.ExamineAndApproveCommand;
using T.Materials.Repository.MaterialsInterface;
using static T.Materials.Domain.Enum.Enums;


namespace T.Materials.Read.API.ApplicationRead.CommandHandler.ExamineAndApproveCommandHadnler
{
    /// <summary>
    /// 物资领用审批命令处理器
    /// </summary>
    public class MaterialRequisitionApproveCommandHandler : IRequestHandler<MaterialRequisitionApproveCommand, ListMaterialRequisitionDto<MaterialRequisition>>
    {
        /// <summary>
        /// 物资领用仓储接口
        /// </summary>
        protected readonly IMaterialRequisitionRepository materialRequisitionRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="materialRequisitionRepository">物资领用仓储接口</param>
        public MaterialRequisitionApproveCommandHandler(IMaterialRequisitionRepository materialRequisitionRepository)
        {
            this.materialRequisitionRepository = materialRequisitionRepository;
        }

        /// <summary>
        /// 处理显示物资领用命令
        /// </summary>
        /// <param name="request">显示物资领用命令</param>
        /// <param name="cancellationToken">取消标记</param>
        /// <returns>物资领用列表</returns>
        public async Task<ListMaterialRequisitionDto<MaterialRequisition>> Handle(MaterialRequisitionApproveCommand request, CancellationToken cancellationToken)
        {
            var list = from a in await materialRequisitionRepository.GetAllAsync()
                        where !a.IsDeleted && a.ApprovalStatus != ApprovalStatus.待提交
                        select new MaterialRequisition
                        {
                            MaterialRequisitionId = a.MaterialRequisitionId,
                            CompanyID = a.CompanyID,
                            User_Id = a.User_Id,
                            Title = a.Title,
                            UsageDescription = a.UsageDescription,
                            Recipient = a.Recipient,
                            MaterialRequisitNotes = a.MaterialRequisitNotes,
                            CollectionDate = a.CollectionDate,
                            ApprovalStatus = a.ApprovalStatus,
                            Approver = a.Approver,
                            CreatedBy = a.CreatedBy,
                            CreatedDate = a.CreatedDate,
                            UpdatedBy = a.UpdatedBy,
                            UpdatedDate = a.UpdatedDate,
                            DeletedBy = a.DeletedBy,
                            DeletedDate = a.DeletedDate,
                            IsDeleted = a.IsDeleted
                        };

            if (!string.IsNullOrEmpty(request.Recipient))
            {
                list = list.Where(x => x.Recipient.Contains(request.Recipient));
            }

            if (request.CollectionDate != null)
            {
                list = list.Where(x => x.CollectionDate == request.CollectionDate);
            }

            if (request.ApprovalStatus != null)
            {
                list = list.Where(x => x.ApprovalStatus == request.ApprovalStatus);
            }

            if (!string.IsNullOrEmpty(request.Approver))
            {
                list = list.Where(x => x.Approver.Contains(request.Approver));
            }

            if (request.pageSize == null)
            {
                request.pageSize = list.Count();
            }
            if (request.pageIndex == null)
            {
                request.pageIndex = 1;
            }

            ListMaterialRequisitionDto<MaterialRequisition> res = new ListMaterialRequisitionDto<MaterialRequisition>();
            var index = request.pageIndex;
            var size = request.pageSize;
            res.totalCount = list.Count();
            res.pageCount = (int)Math.Ceiling((res.totalCount * 1.0) / (int)size);
            res.list = list.OrderByDescending(x => x.CreatedDate).Skip(((int)index - 1) * (int)size).Take((int)size).ToList();
            return res;
        }
    }
}
